package app.entity;

/**
 * 分页实体
 */
public class Pager {
	/** 项目总数 */
	private Integer itemtotal;
	/** 当前页码 */
	private Integer pagenumber;
	/** 单页显示项目数 */
	private Integer pagesize;
	/** 总页码 */
	private Integer pagetotal;

	private Integer pre;// 前一页
	private Integer next;// 后一页

	private Integer stride;// 需要跨过数量

	/***
	 * 构造器
	 * 
	 * @param itemtotal
	 *            总数
	 * @param pagenumber
	 *            页码
	 */
	public Pager(Integer itemtotal, Integer pagenumber) {
		this.itemtotal = itemtotal;
		this.pagenumber = pagenumber;
		this.pagesize = 10;// 默认每页显示10个

		//stride = (pagenumber - 1) * pagesize;

	}

	/***
	 * 构造器
	 * 
	 * @param itemtotal
	 *            总数
	 * @param pagenumber
	 *            页码
	 * @param pagesize
	 *            单页显示项目数
	 */
	public Pager(Integer itemtotal, Integer pagenumber, Integer pagesize) {
		this.itemtotal = itemtotal;
		this.pagenumber = pagenumber;
		this.pagesize = pagesize;
	}

	public Integer getItemtotal() {
		this.itemtotal = this.itemtotal <= 0 ? 0 : this.itemtotal;
		return itemtotal;
	}

	public void setItemtotal(Integer itemtotal) {
		this.itemtotal = itemtotal;
	}

	public Integer getPagenumber() {

		/** 判断当前页面是否超过了总页数:如果超过了默认给最后一页作为当前页 */
		if (pagenumber > getPagetotal()) {
			pagenumber = pagetotal;
		}
		if (pagenumber < 1) {
			pagenumber = 1;
		} // 防止没有内容时pagenumber=0

		return pagenumber;
	}

	public void setPagenumber(Integer pagenumber) {
		this.pagenumber = pagenumber;
	}

	public Integer getPagetotal() {

		pagetotal = itemtotal / pagesize;
		if (pagetotal * pagesize < itemtotal) {
			// 有余数
			pagetotal++;
		}
		// 当前一个栏目有M页面，更改栏目id后N页，M>N
		// 此时需要调整pagenumber
		if (pagetotal < 1) {
			pagetotal = 1;
		}

		return pagetotal;
	}

	public void setPagetotal(Integer pagetotal) {
		this.pagetotal = pagetotal;
	}

	public Integer getPagesize() {
		return pagesize;
	}

	public void setPagesize(Integer pagesize) {
		this.pagesize = pagesize;
	}

	public Integer getPre() {
		pre = pagenumber - 1;
		if (pre < 1) {
			pre = 1;
		}
		return pre;
	}

	public void setPre(Integer pre) {
		this.pre = pre;
	}

	public Integer getNext() {
		next = pagenumber + 1;
		if (next > pagetotal) {
			next = pagetotal;
		}
		if (next == 0) {
			next = 1;
		}
		return next;
	}

	public void setNext(Integer next) {
		this.next = next;
	}

	public Integer getStride() {
		
		stride = (getPagenumber() - 1) * pagesize;		
		//System.out.println("Pager.getStride()stride="+stride);
		return stride;
	}

	public void setStride(Integer stride) {
		//System.out.println("Pager.setStride()");
		this.stride = stride;
	}

}
